package com.wind.sample.dubbo.consumer.controller;

import com.wind.sample.dubbo.consumer.models.UserDTO;
import com.wind.sample.dubbo.proto.user.UserRequest;
import com.wind.sample.dubbo.proto.user.UserResponse;
import com.wind.sample.dubbo.provider.service.GreetingService;
import com.wind.sample.dubbo.provider.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class GreetingController {

    @DubboReference
    private GreetingService greetingService;

    @DubboReference
    private UserService userService;

    @GetMapping("/hello/{name}")
    public String sayHello(@PathVariable("name") String name) {
        log.info("name: {}", name);
        return greetingService.sayHello(name);
    }

    @GetMapping("/getuser/{id}")
    public UserDTO getUser(@PathVariable("id") String id) {
        log.info("user id: {}", id);
        UserRequest request = UserRequest.newBuilder().setId(id).build();
        UserResponse response = userService.getUser(request);
        UserDTO userDTO = UserDTO.builder().id(id)
                .name(response.getName()).age(response.getAge()).build();
        return userDTO;
    }
}
